# -*- Python -*-

# Configuration file for the 'lit' test runner.
import os
import platform
import subprocess

import lit.formats

# name: The name of this test suite.
config.name = 'CPPAMP'

# testFormat: The test format to use to interpret tests.
#
# For now we require '&&' between commands, until they get globally killed and
# the test runner updated.
config.test_format = lit.formats.ShTest(execute_external = True)

# suffixes: A list of file extensions to treat as test files.
config.suffixes = ['.cpp','.ll']

# test_source_root: The root path where tests are located.
config.test_src_root = os.path.dirname(os.path.realpath(__file__))

# target_triple: Used by ShTest and TclTest formats for XFAIL checks.

# only set platform be hsa if HCC_RUNTIME is set as hsa
config.target_triple = platform.system()
if os.environ.get('HCC_RUNTIME'):
    if os.environ['HCC_RUNTIME'] == "HSA":
        config.target_triple = "hsa"
 
if os.environ.get('KM_USE_AMDGPU'):
    config.environment['KM_USE_AMDGPU'] = os.environ['KM_USE_AMDGPU']

if os.environ.get('HCC_AMDGPU_TARGET'):
    config.environment['HCC_AMDGPU_TARGET'] = os.environ['HCC_AMDGPU_TARGET']

# test_source_root: The root path where tests are located.
config.test_source_root = os.path.dirname(__file__)


###
# Check that the site specific configuration exists.
#site_cfg = os.path.join(config.test_source_root, 'lit.site.cfg')
#if not os.path.exists(site_cfg):
#  lit.fatal('No site specific configuration available!')

# Discover the 'clang' and 'clangcc' to use.

def inferClang(PATH):
    # Determine which clang to use.
    clang = os.getenv('CLANG')

    # If the user set clang in the environment, definitely use that and don't
    # try to validate.
    if clang:
        return clang

    # Otherwise look in the path.
    clang = lit.util.which('clang', PATH)

    if not clang:
        lit.fatal("couldn't find 'clang' program, try setting "
                  "CLANG in your environment")

    return clang

cxx_options = ' ' + ' '.join([
  "-I%s" % config.gtest_src_dir,
  "-I%s" % config.opencl_inc_dir,
  "-DGTEST_HAS_TR1_TUPLE=0",
  "-stdlib=libc++",
  subprocess.Popen([
      os.path.join(config.executable_output_path, 'clamp-config'),
      "--build",
      "--cxxflags"],
      stdout=subprocess.PIPE).communicate()[0].rstrip('\n'),
]) + ' '

if platform.system() == 'Darwin':
  link_options = ' ' + ' '.join([
    "-L%s" % config.opencl_lib_dir,
    "-L%s" % config.ampcl_lib_dir,
    "-L%s" % config.mcwamp_lib_dir,
    "-lOpenCL",
    "-lmcwamp",
    ]) + ' '
  gtest_link_options = ' ' + ' '.join([
    "-lmcwamp_gtest",
  ]) + ' '
else:
  link_options = ' ' + ' '.join([
    subprocess.Popen([
        os.path.join(config.executable_output_path, 'clamp-config'),
        "--build",
        "--ldflags"],
        stdout=subprocess.PIPE).communicate()[0].rstrip('\n'),
    "-lpthread",
    ]) + ' '
  gtest_link_options = ' ' + ' '.join([
    "-lmcwamp_gtest",
  ]) + ' '

config.environment['LD_LIBRARY_PATH'] = ':'.join([ \
  config.ampcl_lib_dir,
]) + ":" + config.environment['LD_LIBRARY_PATH']

config.clang = inferClang(config.llvm_tools_dir)
config.clang_cc1 = config.clang + "++"
config.clang_cxx11  = config.clang_cc1 + cxx_options + "-std=c++11"
config.clang_cxxamp = config.clang_cc1 + cxx_options + "-std=c++amp" + link_options
config.clang_hc = config.clang_cc1 + cxx_options + "-hc" + link_options
config.clang_cxxamp_device = config.clang_cxxamp + " -Xclang -famp-is-device -fno-builtin "
config.clang_gtest_amp = config.clang_cxxamp + gtest_link_options

if not lit_config.quiet:
    lit_config.note('using clang: %r' % config.clang)

# Note that when substituting %clang_cc1 also fill in the include directory of
# the builtin headers. Those are part of even a freestanding environment, but
# Clang relies on the driver to locate them.
def getClangBuiltinIncludeDir(clang):
    # FIXME: Rather than just getting the version, we should have clang print
    # out its resource dir here in an easy to scrape form.
    cmd = subprocess.Popen([clang, '-print-file-name=include'],
                           stdout=subprocess.PIPE)
    if not cmd.stdout:
      lit.fatal("Couldn't find the include dir for Clang ('%s')" % clang)
    return cmd.stdout.read().strip()
config.substitutions.append( ('%clang_cc1', '%s -cc1 -internal-isystem %s'
                              % (config.clang, getClangBuiltinIncludeDir(config.clang))) )

config.substitutions.append( ('%ampneg', cxx_options + "-I/usr/local/include -I/usr/include") )
config.substitutions.append( ('%link', link_options) )
config.substitutions.append( ('%cxx11', config.clang_cxx11) )
config.substitutions.append( ('%cxxamp', config.clang_cxxamp) )
config.substitutions.append( ('%hc', config.clang_hc) )
config.substitutions.append( ('%opencl_math_dir', config.opencl_math_dir) )
config.substitutions.append( ('%llvm_libs_dir', config.llvm_libs_dir) )
config.substitutions.append( ('%spirify', os.path.join(config.mcwamp_tool_dir, "clamp-spirify") ) )
config.substitutions.append( ('%clamp-device', os.path.join(config.mcwamp_tool_dir, "clamp-device") ) )
config.substitutions.append( ('%amp_device', config.clang_cxxamp_device) )
config.substitutions.append( ('%gtest_amp', config.clang_gtest_amp) )
config.substitutions.append( ('%FileCheck', os.path.join(config.llvm_tools_dir,'FileCheck') ) )
config.substitutions.append( ('%llc', os.path.join(config.llvm_tools_dir,'llc') ) )
config.substitutions.append( ('%llvm-as', os.path.join(config.llvm_tools_dir, 'llvm-as') ) )
config.substitutions.append( ('%llvm-dis', os.path.join(config.llvm_tools_dir, 'llvm-dis') ) )
config.substitutions.append( ('%opt', os.path.join(config.llvm_tools_dir, 'opt') ) )
config.substitutions.append( ('%clang', config.clang) )
config.substitutions.append( ('%cppfilt', 'c++filt -n') )
config.substitutions.append( ('%embed_kernel', os.path.join(config.mcwamp_tool_dir, "clamp-embed") ) )
config.substitutions.append( ('%hsail', os.path.join(config.mcwamp_tool_dir, "clamp-hsail") ) )
config.substitutions.append( ('%clamp-preprocess', os.path.join(config.llvm_tools_dir, "clamp-preprocess") ) )

